<template>
  <div class="dashboard-container">
    <div class="container" style="background-color: azure;">
      <div class="tableBar" style="background-color: azure;">
        <label style="margin-left:680px; margin-right: 5px;">员工姓名：</label>
        <el-input 
          v-model="input" 
          placeholder="请输入员工姓名"
          style="width: 15%;"
          clearable
          @clear="init"
          @keyup.enter.native="initFun"
        />
        <el-button class="normal-btn continue" @click="init(true)">
          查询
        </el-button>
        <el-button
          type="danger"
          style="float: left"
          @click="addEmployeeHandle('add')"
        >
          + 添加员工
        </el-button>
      </div>

      
      <el-table
        :row-class-name="tableRowClassName"
        v-if="tableData.length"
        :data="tableData"
        border
        style="width: 100%"
        class="tableBox"
      >
        <el-table-column prop="name" label="员工姓名" >
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="bottom" :popover-class="'custom-popover-color'">
              <p>员工姓名: {{ scope.row.name }}</p>
              <p>员工账号: {{ scope.row.username }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium" effect="light" style="color:#aaa"
              >{{ scope.row.name }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="账号"  />
        <el-table-column prop="phone" label="手机号"  />
        <el-table-column label="账号状态" 
                  prop="tag" 
                  :filters="[{ text: '禁用', value: String(0) }, { text: '启用', value: String(1) }]"
                  :filter-method="filterTag"
                  filter-placement="bottom-end">
                <template slot-scope="scope">
                  <div
                    class="tableColumn-status"
                    :class="{ 'stop-use': String(scope.row.status) === '0' }"
                  >
                  <el-tag
                    :type="String(scope.row.status) === '1' ? 'success' : 'primary'"
                    disable-transitions>
                    {{ String(scope.row.status) === '0' ? '禁用' : '启用' }}
                  </el-tag>    
                  </div>
                </template>
              </el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间" sortable >

          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center" width=320>
          <template slot-scope="scope">
            <el-button
              size="small"
              class="blueBug"
              :class="{ 'disabled-text': scope.row.username === 'admin' }"
              :disabled="scope.row.username === 'admin'"
              @click="addEmployeeHandle(scope.row.id, scope.row.username)"
            >
              修改
            </el-button>
            <el-button
              :disabled="scope.row.username === 'admin'"
              size="small"
              class="non"
              :class="{
                'disabled-text': scope.row.username === 'admin',
                blueBug: scope.row.status == '0',
                delBut: scope.row.status != '0'
              }"
              @click="statusHandle(scope.row)"
            >
              {{ scope.row.status == '1' ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Empty v-else :is-search="isSearch" />
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { Component, Vue } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import { getEmployeeList, enableOrDisableEmployee } from '@/api/employee'
import { UserModule } from '@/store/modules/user'
import InputAutoComplete from '@/components/InputAutoComplete/index.vue'
import Empty from '@/components/Empty/index.vue'

@Component({
  name: 'Employee',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty,
  },
})
export default class extends Vue {
   input = ''
   counts = 0
   page = 1
   pageSize = 10
   tableData = []
   id = ''
   status = ''
   isSearch = false

  created() {
    this.init()
  }

  initProp(val) {
    this.input = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }

  get userName() {
    return UserModule.username
  }

   async init(isSearch) {
    this.isSearch = isSearch
    const params = {
      page: this.page,
      pageSize: this.pageSize,
      name: this.input ? this.input : undefined,
    }
    await getEmployeeList(params)
      .then((res) => {
        if (String(res.data.code) === '1') {
          this.tableData = res.data && res.data.data && res.data.data.records
          this.counts = res.data.data.total
        }
        // if (!res.data.data.records.length && type === 'search') {
        //   this.$message.error('未搜索到相关员工，请核对员工姓名是否正确')
        // }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message)
      })
  }

  // 添加
   addEmployeeHandle(st, username) {
    if (st === 'add') {
      this.$router.push({ path: '/employee/add' })
    } else {
      if (username === 'admin') {
        return
      }
      this.$router.push({ path: '/employee/add', query: { id: st } })
    }
  }

  //状态修改
   statusHandle(row) {
    if (row.username === 'admin') {
      return
    }
    this.id = row.id
    this.status = row.status
    this.$confirm('确认调整该账号的状态?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      enableOrDisableEmployee({ id: this.id, status: !this.status ? 1 : 0 })
        .then((res) => {
          if (String(res.status) === '200') {
            this.$message.success('账号状态更改成功！')
            this.init()
          }
        })
        .catch((err) => {
          this.$message.error('请求出错了：' + err.message)
        })
    })
  }

  handleSizeChange(val) {
    this.pageSize = val
    this.init()
  }

  handleCurrentChange(val) {
    this.page = val
    this.init()
  }

  filterTag(value, row) {
        console.log("String(value)=",String(value));
        console.log("row.status=",row.status);
        return row.status == String(value);
  }

  tableRowClassName({row,value}){
    if(String(row.status) == '0'){
      return 'warning-row';
    }else if(String(row.status) == '1'){
      return 'success-row';
    }else{
      return 'default-row';
    }
  }

}
</script>

<style lang="scss" scoped>
.disabled-text {
  color: #bac0cd !important;
}
.custom-popover-color {
  background-color: rgb(217, 253, 255);
  color: rgb(217, 253, 255);
}
</style>
